package com.dongbao.util;

import com.dongbao.core.config.MyState;
import com.dongbao.entity.DaoIsDelete;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ToolUtil {
    /**
     * 拷贝到剪贴板
     *
     * @param text 文本
     * @author Kings
     * @date 2021/06/07
     */
    public static void setClipboardString(String text) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(text);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }
    /**
     * 检测文本中是否有中文汉子存在
     *
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;

    }

    /**
     * 对象是否不为空(新增)
     *
     * @param o String,List,Map,Object[],int[],long[]
     * @return
     */
    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    /**
     * 对象是否为空
     *
     * @param o String,List,Map,Object[],int[],long[]
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            return o.toString().trim().equals("");
        } else if (o instanceof java.util.List) {
            return ((List) o).size() == 0;
        } else if (o instanceof Map) {
            return ((Map) o).size() == 0;
        } else if (o instanceof Set) {
            return ((Set) o).size() == 0;
        } else if (o instanceof Object[]) {
            return ((Object[]) o).length == 0;
        } else if (o instanceof int[]) {
            return ((int[]) o).length == 0;
        } else if (o instanceof long[]) {
            return ((long[]) o).length == 0;
        }
        return false;
    }

    /**
     * 获取启用的验证表的集合
     */
    public static  List<DaoIsDelete> getStartDaoIsDelete() {
        StorageConfig config = MyState.getInstance().getConfig();
        String[][] daoFileCheckTableNames = config.getDaoFileCheckTableNames();
        //过滤出启用的表
        List<DaoIsDelete> daoIsDeletes = new ArrayList<DaoIsDelete>();
        for(String[] row:daoFileCheckTableNames){
            if("1".equals(row[3])){
                //启用的,提取出表名
                DaoIsDelete daoIsDelete = new DaoIsDelete();
                daoIsDelete.setTableName(row[1]);
                daoIsDelete.setTableCode(row[2]);
                daoIsDelete.setStatus(row[3]);
                daoIsDeletes.add(daoIsDelete);
            }
        }
        return daoIsDeletes;
    }

}
